﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Accounts;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using Android.Content.Res;
using Android.Util;
using MWC.BL;

using BugSense;

namespace MWC.NetcampCheckin
{
    // Dodalem intentfiltry do otwarcia naszej aplikacji po klinieciu w link netcamp://info
    [Activity(Label = "Netcamp Autoryzacja")]
     [IntentFilter(new[] { Intent.ActionView }, DataScheme = "http", DataHost = "www.netcamp.pl", DataPathPattern ="/mobileauth.*", Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable })]
    class NetcampLogin : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            try
            {
                base.OnCreate(bundle);

                BugSenseHandler.Instance.ClearCrashExtraData();
                BugSense.Core.Model.LimitedCrashExtraDataList extras = BugSenseHandler.Instance.CrashExtraData;

                extras.Add(new BugSense.Core.Model.CrashExtraData
                {
                    Key = "Screen",
                    Value = "NetcampLogin"
                });

                string parameter = null;
                var prefs = GetSharedPreferences("NetcampApp", 0);
                if (Intent.GetBooleanExtra("LastActivity", false) == false)
                {
                    parameter = prefs.GetString("Hash", "");
                    //usun niepotrzebne dane parametru
                    parameter = parameter.Replace("hash=", "");
                }
                SetContentView(Resource.Layout.NetcampLogin);


                var loginText = prefs.GetString("Email", "");
                var hash = prefs.GetString("Hash", "");

                //nawiasy, caly opis + boldTextOfSubTitle
                TextView[] tabOfBoldItems = new TextView[3];
                tabOfBoldItems[0] = FindViewById<TextView>(Resource.Id.firstParenthasesOfTitle);
                tabOfBoldItems[1] = FindViewById<TextView>(Resource.Id.secondParanthasesOfTitle);
                tabOfBoldItems[2] = FindViewById<TextView>(Resource.Id.boldTextOfSubtitle);

                setFont("fonts/texgyreheros-regular-webfont.ttf", tabOfBoldItems, TypefaceStyle.Bold);

                TextView[] tabOfNormalItems = new TextView[2];
                int iterator = 0;

                var passwordField = FindViewById<TextView>(Resource.Id.Password);
                TextView loginInformation = FindViewById<TextView>(Resource.Id.loginInformation);
                TextView authorizationCode = FindViewById<TextView>(Resource.Id.authorizationCode);
                if (parameter != null)
                {
                    loginInformation.Text = "Kliknij zatwierdź aby zalogować się do systemu";
                    passwordField.Text = parameter;
                }

                tabOfNormalItems[iterator++] = passwordField;
                tabOfNormalItems[iterator++] = FindViewById<TextView>(Resource.Id.SignIn);

                setFont("fonts/texgyreheros-regular-webfont.ttf", tabOfNormalItems, TypefaceStyle.Normal);

                Button signIn = FindViewById<Button>(Resource.Id.SignIn);

                signIn.Click += delegate
                {
                    if (passwordField.Text == hash)
                    {
                        //MWCApp.LogDebug("Uruchom NetcampCountTheDistance");
                        var prefsEditor = prefs.Edit();
                        prefsEditor.PutInt("CurrentStep", 2);
                        var netcampCountTheDistance = new Intent(this, typeof(NetcampCountTheDistance));
                        netcampCountTheDistance.PutExtra("FirstData", tabOfNormalItems[0].Text.ToString());
                        //MWCApp.LogDebug("StartActivity(netcampCountTheDistance);");
                        StartActivity(netcampCountTheDistance);
                        Finish();
                    }
                    else
                        Toast.MakeText(this, "Nieprawidlowe haslo!", ToastLength.Long).Show();
                };
            }
            catch (Exception e)
            {
                MWCApp.LogDebug(e);
                NetcampAlerts.UnknownError(this);
            }


        }

        // 1 - sciezka do czcionki, 2 - tablica, 3 - Typ:Bold,Italic,Normal,BoldItalic
        private void setFont(string path, TextView[] tTV, TypefaceStyle Type)
        {
            //Android.Content.Res.AssetManager mgr;
            AssetManager mgr;
            mgr = Assets;
            Typeface font = Typeface.CreateFromAsset(mgr, path);
            for (int i = 0; i < tTV.Length; i++)
            {
                tTV[i].SetTypeface(font, Type);
            }

        }




    }
}